<?php


namespace api\web\controller;


use cmf\controller\RestBaseController;
use api\web\model\NewsModel;

class NewsController extends  RestBaseController
{
    public function index(){
        //获取搜索字段
        $key = input('key');
        $page = input('page/d',1);
        $listRow = input('list_row/d',10);
        $orderField = input('order_field/s','list_order');
        $orderType = input('order_type/s','DESC');
        $where = [];
        //判断是否有值
        if($key){
            $where[] = ['author|title', 'like', '%' . $key . '%'];
        }
        $list = NewsModel::order([$orderField => $orderType])
            ->field('id,title,author,image,abstract,look_num,create_time')
            ->where($where)
            ->page($page,$listRow)->select();
        $this->success('新闻列表',$list);
    }
    //显示内容
    public function getContent(){
        $id = input('id/d');
        if(empty($id)){
            $this->error('参数错误');
        }
        $news =  NewsModel::withoutField('delete_time,list_order')->find($id);
        if(!$news){
            $this->error('新闻不存在');
        }
        $news['content'] = cmf_replace_content_file_url(htmlspecialchars_decode($news['content']));
        $this->success('新闻详情',$news);
    }
}